using System.Collections;
using UnityEngine;

namespace Spine.Unity.Examples 
{
    /// <summary>
    /// 角色身体状态枚举
    /// 定义角色的基础行为状态
    /// </summary>
    public enum SpineBeginnerBodyState {
        Idle,    // 空闲状态
        Running, // 奔跑状态
        Jumping  // 跳跃状态
    }
    
    /// <summary>
    /// Spineboy基础行为模型类
    /// 管理角色状态、输入响应和动画状态转换
    /// </summary>
    [SelectionBase] // 使该对象在Unity编辑器中优先被选中
    public class SpineboyBeginnerModel : MonoBehaviour 
    {
        #region Inspector 配置区域
        [Header("当前状态")]
        [Tooltip("当前身体状态")]
        public SpineBeginnerBodyState state;
        
        [Tooltip("是否面朝左侧")]
        public bool facingLeft;
        
        [Range(-1f, 1f)]
        [Tooltip("当前移动速度")]
        public float currentSpeed;

        [Header("跳跃平衡设置")]
        [Tooltip("射击间隔时间(秒)")]
        public float shootInterval = 0.12f;
        #endregion

        private float lastShootTime;
        
        // 事件系统：允许其他脚本订阅角色行为事件
        public event System.Action ShootEvent;      // 触发射击动作时调用
        public event System.Action StartAimEvent;   // 开始瞄准时调用
        public event System.Action StopAimEvent;    // 停止瞄准时调用

        #region 公共API
        /// <summary>
        /// 尝试执行跳跃动作
        /// </summary>
        public void TryJump () {
            StartCoroutine(JumpRoutine());
        }

        /// <summary>
        /// 尝试执行射击动作
        /// </summary>
        public void TryShoot () {
            float currentTime = Time.time;
            
            // 检查射击间隔时间
            if (currentTime - lastShootTime > shootInterval) {
                lastShootTime = currentTime;
                ShootEvent?.Invoke();  // 触发射击事件
            }
        }

        /// <summary>
        /// 开始瞄准动作
        /// </summary>
        public void StartAim () {
            StartAimEvent?.Invoke();  // 触发瞄准开始事件
        }

        /// <summary>
        /// 停止瞄准动作
        /// </summary>
        public void StopAim () {
            StopAimEvent?.Invoke();   // 触发瞄准结束事件
        }

        /// <summary>
        /// 处理角色移动输入
        /// </summary>
        /// <param name="speed">移动速度(-1左移至1右移)</param>
        public void TryMove (float speed) {
            currentSpeed = speed;

            // 更新角色朝向
            if (speed != 0) {
                bool speedIsNegative = (speed < 0f);
                facingLeft = speedIsNegative; 
            }

            // 更新动画状态
            if (state != SpineBeginnerBodyState.Jumping) {
                state = (speed == 0) ? SpineBeginnerBodyState.Idle : SpineBeginnerBodyState.Running;
            }
        }
        #endregion

        #region 协程：跳跃动作
        IEnumerator JumpRoutine () {
            // 防止重复跳跃
            if (state == SpineBeginnerBodyState.Jumping) yield break;

            state = SpineBeginnerBodyState.Jumping;

            Vector3 startPos = transform.localPosition;
            const float jumpHeight = 20f;
            const float jumpDuration = 1.2f;

            // 上升阶段
            for(float t = 0; t < jumpDuration/2; t += Time.deltaTime) {
                float yOffset = jumpHeight * (1 - (t*2)/jumpDuration);
                transform.Translate(Vector3.up * (yOffset * Time.deltaTime));
                yield return null;
            }

            // 下降阶段
            for(float t = 0; t < jumpDuration/2; t += Time.deltaTime) {
                float yOffset = jumpHeight * (t*2)/jumpDuration;
                transform.Translate(Vector3.down * (yOffset * Time.deltaTime));
                yield return null;
            }

            transform.localPosition = startPos;
            state = SpineBeginnerBodyState.Idle;
        }
        #endregion
    }
}